package blacklist

import (
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/app"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/database"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/paginator"

	"github.com/gofiber/fiber/v2"
)

func Get(id uint64) (blacklist Blacklist) {
	database.DB.Where("id", id).First(&blacklist)
	return
}

func GetBlackItemByBlackId(userId uint64, blackId uint64) (blacklist Blacklist) {
	database.DB.Where("user_id = ?", userId).Where("black_id = ?", blackId).First(&blacklist)
	return
}

func GetBy(field, value string) (blacklist Blacklist) {
	database.DB.Where("? = ?", field, value).First(&blacklist)
	return
}

func All() (blacklists []Blacklist) {
	database.DB.Order("id desc").Find(&blacklists)
	return
}

func GetBlacklistByUserIdForPassenger(userId uint64) (blacklists []Blacklist) {
	return GetBlacklistByUserIdAndType(userId, PASSENGER)
}

func GetBlacklistByUserIdForDriver(userId uint64) (blacklists []Blacklist) {
	return GetBlacklistByUserIdAndType(userId, DRIVER)
}

func GetBlacklistByUserIdAndType(userId uint64, blackType uint8) (blacklists []Blacklist) {
	database.DB.Where("user_id = ? and type = ?", userId, blackType).Order("id desc").Find(&blacklists)
	return
}

func GetBlackCountByUserIdForPassenger(userId uint64) (count int64) {
	return GetBlacklistCountByUserIdAndType(userId, PASSENGER)
}

func GetBlackCountByUserIdForDriver(userId uint64) (count int64) {
	return GetBlacklistCountByUserIdAndType(userId, DRIVER)
}

func GetBlacklistCountByUserIdAndType(userId uint64, blackType uint8) (count int64) {
	database.DB.Model(Blacklist{}).Where("user_id = ? and type = ?", userId, blackType).Count(&count)
	return
}

func IsExist(field, value string) bool {
	var count int64
	database.DB.Model(Blacklist{}).Where("? = ?", field, value).Count(&count)
	return count > 0
}

// Paginate 分页内容
func Paginate(c *fiber.Ctx, perPage int) (blacklists []Blacklist, paging paginator.Paging) {
	paging = paginator.Paginate(
		c,
		database.DB.Model(Blacklist{}),
		&blacklists,
		app.V1URL(database.TableName(&Blacklist{})),
		perPage,
	)
	return
}
